package com.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.zpark.entity.User;
import com.zpark.service.IUserService;
import com.zpark.utils.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口：UserController")
public class UserController {

    @Autowired
    private IUserService userService;

    private static final Integer STATUS_CODE = 20000;

    private static final String SUCCESS = "OK";

    private static final String ERROR = "ERROR";


    // 新增数据
    @PostMapping("/add")
    @ApiOperation("添加用户数据接口")
    public JsonUtil<Integer> addUser(User user) {
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.addUser(user));
    }

    // 删除数据
    @PostMapping("/del")
    @ApiOperation("删除用户数据接口")
    @ApiImplicitParam(name = "id", required = true, value = "用户id")
    public JsonUtil<Integer> delUserById(Integer id){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.delUserById(id));
    }

    // 修改数据
    @PostMapping("/update")
    @ApiOperation("修改用户数据接口")
    public JsonUtil<Integer> changeUserUnameById(User user){
        return new JsonUtil<>(STATUS_CODE, SUCCESS,userService.changeUser(user));
    }

    // 查询所有用户数据
    @GetMapping("/all")
    @ApiOperation("查询所有用户数据接口")
    public JsonUtil<List<User>>  findAll(){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.findAll());
    }

    // 根据ID查询用户
    @GetMapping("/find_id")
    @ApiOperation("根据用户id查询用户数据接口")
    @ApiImplicitParam(name = "id", required = true, value = "用户id")
    public JsonUtil<User> findById(Integer id){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.findById(id));
    }

    @GetMapping("/find_like/{name}")
    @ApiOperation("根据名字模糊查询用户数据接口")
    @ApiImplicitParam(name = "uname", required = true, value = "用户的名字", paramType = "path")
    public JsonUtil<List<User>>  findLike(@PathVariable("name") String uname){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.findByUnameLike(uname));
    }

    @GetMapping("/find_limit")
    @ApiOperation("分页查询用户数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageStarter", value = "起始页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页展示数据大小",
                    required = false, defaultValue = "2")
    })
    public JsonUtil<PageInfo<User>>  findLimit(Integer pageStarter, Integer pageSize){

        return new JsonUtil<>(STATUS_CODE, SUCCESS,
                userService.selectUserLimit(pageStarter, pageSize));
    }
}